package com.itic.appbase.framework.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity; 
import org.apache.http.client.config.RequestConfig; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.CloseableHttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClientBuilder; 



public class Test {
	public static void main(String[] args) throws ParseException, UnsupportedEncodingException, IOException {
		
		//JSONObject params = new JSONObject();

		Map<String, Object> params = new HashMap<>();
		
		params.put("appkey", "68943923115886070418838901844741");
		params.put("shopkey", "e7c5289b-07ee-492f-b84f-97ee9659c8b7");
		params.put("sign_key", "lezitiancheng ");
		params.put("token", "aoff7pvcJ7ycMa7lyTnUwCVmPey4Ylksv0Pq3PXh");
		params.put("method", "beefun.selfbuiltmall.uploadsaleorders");
		params.put("timestamp", "2018-03-13 07:23:30");
		String sign = MD5Helper.md5("lezitiancheng ");
		params.put("sign", sign);
		JSONObject ordersObj = new JSONObject();
		ordersObj.put("buyerMessage", "订单测试");
		ordersObj.put("invoiceTitle", "发票抬头");
		ordersObj.put("sellerMemo", "卖家备注");
		ordersObj.put("tradeCreateTime", "2015-11-06 13:23:30");
		ordersObj.put("tradeFinishTime", "2015-11-06 13:23:30");
		ordersObj.put("tradeModifiedTime", "2015-11-06 13:23:30");
		ordersObj.put("tradePaiedTime", "2015-11-06 13:23:30");
		ordersObj.put("tradeStatus", "2");
		ordersObj.put("tradeTotal", "600");
		ordersObj.put("tradeType", "0");
		ordersObj.put("tradeId", "DD-20180131-001");
		ordersObj.put("total", "320");
		ordersObj.put("preferentialTotal", "280");
		ordersObj.put("invoiceCode", "123456789");
		ordersObj.put("mallSendType", "0");
		JSONObject eShopBuyerObj = new JSONObject();
		eShopBuyerObj.put("eShopBuyer", "1281147699@qq.com");
		eShopBuyerObj.put("customerReceiver", "123456");
		eShopBuyerObj.put("customerReceiverAddress", "天府软件园D2");
		eShopBuyerObj.put("customerReceiverCity", "成都市");
		eShopBuyerObj.put("customerReceiverCountry", "中国");
		eShopBuyerObj.put("customerReceiverDistrict", "高新区");
		eShopBuyerObj.put("customerReceiverMobile", "12345678910");
		eShopBuyerObj.put("customerReceiverPhone", "028-1234567");
		eShopBuyerObj.put("customerReceiverProvince", "四川省");
		eShopBuyerObj.put("customerReceiverZipcode", "");
		eShopBuyerObj.put("customerShopAccount", "zzz");
		
		ordersObj.put("eShopBuyer", eShopBuyerObj.toString());
		JSONArray orderdetails = new JSONArray();
		JSONObject orderDetail = new JSONObject();
		orderDetail.put("productName", "测试商品");
		orderDetail.put("ptypeId", "110890-9001");
		orderDetail.put("qty", "1");
		orderDetail.put("price", "70");
		orderDetail.put("skuId", "110890-9528");
		orderDetail.put("tradeOriginalPrice", "100");
		orderDetail.put("preferentialTotal", "30");
		orderDetail.put("platformPropertiesName", "属性1");
		orderdetails.add(orderDetail);
		ordersObj.put("orderdetails", orderdetails.toString());
		params.put("orders",ordersObj.toString());
		
		StringBuffer sb = new StringBuffer("http://ca.mygjp.com:8010/api?");
		String url = "http://ca.mygjp.com:8010/api";
		for(String key :params.keySet())
			sb.append(key).append("=").append(params.get(key)).append("&");
		//JSONObject obj = HttpRequestUtils.httpPost("http://ca.mygjp.com:8010/api", params);
		 if (params != null && !params.isEmpty()) {
			 List<NameValuePair>pairs = new ArrayList<>(params.size());
	            for (String key : params.keySet()) {
	                    pairs.add(new BasicNameValuePair(key, params.get(key).toString()));
	            }
	            url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, "UTF-8"));
	        }
		JSONObject obj = HttpRequestUtils.httpGetJSONObject(url);
		System.out.println(obj.toString());
		System.out.println(sb.toString());
		System.out.println(sb.substring(159));
	}

}
